package com.jinwei.qunguanjia.user.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.jinwei.qunguanjia.common.constant.UserConstants;
import com.jinwei.qunguanjia.common.core.domain.model.Condition;
import com.jinwei.qunguanjia.common.core.page.PageDataInfo;
import com.jinwei.qunguanjia.common.utils.*;
import com.jinwei.qunguanjia.user.dao.UserMessageDAO;
import com.jinwei.qunguanjia.user.dto.UserMessageDTO;
import com.jinwei.qunguanjia.user.po.UserMessagePO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service("userMessageService")
public class UserMessageServiceImpl implements UserMessageService {

    @Resource
    private UserMessageDAO userMessageDAO;

    @Override
    public String save(UserMessageDTO userMessageDTO) {
        UserMessagePO userMessagePO = new UserMessagePO();
        BeanUtils.copyProperties(userMessageDTO, userMessagePO);
        String userMessageId = IdUtils.getWorkerId();
        userMessagePO.setMessageId(userMessageId);
        userMessagePO.setCreateTime(DateUtils.getDateTime());
        userMessageDAO.insert(userMessagePO);
        return userMessageId;
    }

    @Override
    public void remove(String id) {
        userMessageDAO.deleteById(id);
    }

    @Override
    public void update(UserMessageDTO userMessageDTO) {
        UserMessagePO userMessagePO = new UserMessagePO();
        BeanUtils.copyProperties(userMessageDTO, userMessagePO);
        userMessagePO.setUpdateTime(DateUtils.getDateTime());
        userMessageDAO.updateById(userMessagePO);
    }

    @Override
    public UserMessageDTO get(String id) {
        UserMessagePO userMessagePO = userMessageDAO.selectById(id);
        if (null == userMessagePO) {
            return null;
        }
        UserMessageDTO userMessageDTO = new UserMessageDTO();
        BeanUtils.copyProperties(userMessagePO, userMessageDTO);
        return userMessageDTO;
    }

    @Override
    public UserMessageDTO get(UserMessageDTO userMessageDTO) throws IllegalAccessException {
        // 添加查询条件
        List<Condition> conditionList = ConditionUtils.add(userMessageDTO);
        // 拼接查询条件
        QueryWrapper<UserMessagePO> queryWrapper = SearchUtils.parseWhereSql(conditionList);
        UserMessagePO userMessagePO = userMessageDAO.selectOne(queryWrapper);
        if (null == userMessagePO) {
            return null;
        }
        BeanUtils.copyProperties(userMessagePO, userMessageDTO);
        return userMessageDTO;
    }

    @Override
    public Long count(UserMessageDTO userMessageDTO) throws IllegalAccessException {
        // 添加查询条件
        List<Condition> conditionList = ConditionUtils.add(userMessageDTO);
        // 拼接查询条件
        QueryWrapper<UserMessagePO> queryWrapper = SearchUtils.parseWhereSql(conditionList);
        return userMessageDAO.selectCount(queryWrapper);
    }

    @Override
    public List<UserMessageDTO> list(UserMessageDTO userMessageDTO) throws IllegalAccessException {
        // 添加查询条件
        List<Condition> conditionList = ConditionUtils.add(userMessageDTO);
        // 拼接查询条件
        QueryWrapper<UserMessagePO> queryWrapper = SearchUtils.parseWhereSql(conditionList);
        return JsonUtils.copyList(userMessageDAO.selectList(queryWrapper), UserMessageDTO.class);
    }

    @Override
    public PageDataInfo<UserMessageDTO> listPage(UserMessageDTO userMessageDTO) throws IllegalAccessException {
        // 添加查询条件
        List<Condition> conditionList = ConditionUtils.add(userMessageDTO);
        // 拼接查询条件
        QueryWrapper<UserMessagePO> queryWrapper = SearchUtils.parseWhereSql(conditionList);
        List<UserMessagePO> list = userMessageDAO.selectList(queryWrapper);
        PageInfo<UserMessagePO> pageInfo = new PageInfo<>(list);
        PageDataInfo<UserMessageDTO> pageDataInfo = new PageDataInfo<>();
        pageDataInfo.setData(JsonUtils.copyList(list, UserMessageDTO.class));
        pageDataInfo.setTotal(pageInfo.getTotal());
        pageDataInfo.setTotalPage(PageCountUtils.getPageCount(pageInfo.getTotal(), pageInfo.getPageSize()));
        return pageDataInfo;
    }

    @Override
    public String checkUnique(UserMessageDTO userMessageDTO) throws IllegalAccessException {
        String userMessageId = StringUtils.isNull(userMessageDTO.getMessageId()) ? "-1" : userMessageDTO.getMessageId();
        UserMessageDTO info = get(userMessageDTO);
        if (StringUtils.isNotNull(info) && !info.getMessageId().equals(userMessageId)) {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }
}
